<?php

namespace App\Http\Controllers\Api;

use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use App\Http\Requests\SuggestResources;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class SuggestController extends Controller
{
    //添加建议
    public function add(SuggestResources $request){

    	$input = $request->input();

    	$path = !empty($request->file('suggest_img'))?$request->file('suggest_img'):[];
 		
 		$img_path = [];
    	foreach ($path as $key => $value) {
    		$img_path[] = $value->store('public/upload');
    	}

    	$path = implode($img_path, ",");

    	if(!empty($input['image_path'])){
    	    $path = $input['image_path'];
        }

    	DB::table('suggest')->insert([
		    ['time' => time(), 'uid' => $request->user()->id,'complainant'=>$input['suggest_name'],'suggest'=>$input['suggest_title'],'complaint'=>$input['suggest_content'],'image'=>$path]
		]);

    	return response()->json(['message' => '访问成功！.','code'=>'1']);
    }

    //上传图片接口
    public function add_image(Request $request){
        $path = !empty($request->file('suggest_img'))?$request->file('suggest_img'):[];

        $path = $path->store('public/upload');

        return response()->json(['message' => $path,'code'=>'1']);
    }
}
